//
//  DHNetworkManager+HotelNetwork.m
//  精品酒店
//
//  Created by DIANGHONG on 2018/5/7.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "DHNetworkManager+HotelNetwork.h"

@implementation DHNetworkManager (HotelNetwork)

/*
 获取手机验证码的网络请求
 **/
- (void)getPhoneConfirmCode:(NSString *)phoneNumber completion:(void(^)(NSString *statusInfo, BOOL isSuccess))completion
{
    if ([phoneNumber isEqualToString:@""])
    {
        completion(@"手机号码为空",false);
        return;
    }
    NSString *urlStr = @"http://jpjd.cnaite.cn/mobile/index.php?act=member_secret&op=get_secret";
    
    NSDictionary *params = @{@"phone":phoneNumber};
    
    [self requestWithMethod:MethodPost UrlString:urlStr params:params completion:^(id json, BOOL isSuccess)
     {
         if (!isSuccess){ completion(@"网络有问题",false); return ;}
         
         
         if (json[@"datas"][@"error"]){ completion(json[@"datas"][@"error"],false); return;}
         
         completion(json[@"datas"][@"ex_time"],true);
     }];
}
#pragma mark: - 我的 控制器网络请求
- (void)requestMineInfoWithKey:(NSString *)key completion:(void(^)(DHMemberInfoModel *memberInfo, BOOL isSuccess))completion
{
    NSString *urlStr = @"http://jpjd.cnaite.cn/mobile/index.php?act=member_index";
    
    NSDictionary *param = @{@"key":key};
    
    [self requestWithMethod:MethodGet UrlString:urlStr params:param completion:^(id json, BOOL isSuccess) {
        if (!isSuccess) {
            completion(nil,false);
            return ;
        }
        if (!json[@"datas"][@"member_info"]) {
            completion(nil,false);
            return;
        }
        DHMemberInfoModel *memberInfo = [DHMemberInfoModel yy_modelWithJSON:json[@"datas"][@"member_info"]];
        completion(memberInfo,isSuccess);
    }];
}
/*
获取清洁区域
 **/
#pragma mark: - 获取清洁区域
- (void)loadClearAreaWithKey:(NSString *)key
                  completion:(void(^)(id json, BOOL isSuccess))completion
{
    NSString *urlstr = @"http://jpjd.cnaite.cn/mobile/index.php?act=hotel_cleaning&op=getClearArea";
    
    NSDictionary *params = @{@"key":key};
    
    [DHNetworkManager.shareManager requestWithMethod:MethodGet UrlString:urlstr params:params
                                          completion:^(id json, BOOL isSuccess)
    {
        if (!isSuccess) {
            completion(nil,false);
            return ;
        }
        
        completion(json,isSuccess);
    }];
}
/*
 订单列表网络请求
 **/
- (void)requestHomeListWithPage:(NSString *)page
                            lng:(NSString *)lng
                            lat:(NSString *)lat
                      area_name:(NSString *)area_name
                         params:(NSDictionary *)params
                     completion:(void(^)(id json, BOOL isSuccess))completion
{
    NSMutableDictionary *param = params ? [NSMutableDictionary dictionaryWithDictionary:params] : [NSMutableDictionary dictionary];
    if (page) {
        [param setObject:page forKey:@"page"];
    }
    if (lng) {
         [param setObject:lng forKey:@"lng"];
    }
    if (lat) {
        [param setObject:lat forKey:@"lat"];
    }
    if (area_name) {
        [param setObject:area_name forKey:@"area_name"];
    }
    NSString *urlStr = @"http://jpjd.cnaite.cn/mobile/index.php?act=cleaning_info&op=getTaskList";
    
    
    [self requestWithMethod:MethodGet UrlString:urlStr params:param completion:^(id json, BOOL isSuccess)
    {
        completion(json,isSuccess);
    }];
}
/*
 会员认证网络请求
 **/
- (void)postMemberAuthodWithParams:(NSDictionary *)params
                finished:(void(^)(BOOL isSuccess, DHAuthodDatasModel *datasModel, NSString *error))finished
{
    NSString *url = @"http://jpjd.cnaite.cn/mobile/index.php?act=member_index&op=member_info_auth";
    
//    NSMutableDictionary *pa = [NSMutableDictionary dictionary];
//    pa[@"key"] = @"b0d0b6f4e9a6e87995eda6429c34f6e5";
    [self requestWithMethod:MethodPost UrlString:url params:params
                 completion:^(id json, BOOL isSuccess)
    {
        if (!isSuccess)
        {
            finished(false,nil,@"请检查网路");
            return ;
        }
        if (json[@"datas"][@"error"]) {
            finished(YES,nil,json[@"datas"][@"error"]);
            return;
        }
        DHAuthodDatasModel *datasModel = [DHAuthodDatasModel yy_modelWithJSON:json[@"datas"]];
        finished(isSuccess,datasModel,json[@"datas"][@"succ"]);
    }];
    
}
#pragma mark: - 发布任务网络请求
- (void)postClearTaskWithParam:(NSDictionary *)param completion:(void (^)(BOOL isSuccess, NSString *))completion
{
    NSString *url = @"http://jpjd.cnaite.cn/mobile/index.php?act=hotel_cleaning&op=addClearTask";
    
    [self requestWithMethod:MethodPost UrlString:url params:param completion:^(id json, BOOL isSuccess)
     {
         NSLog(@"%@",json);
    }];
}
/*
 预约任务
 **/
- (void)postTaskWithParam:(NSDictionary *)param
               completion:(void(^)(BOOL isSuccess, NSString *statusInfo))completion
{
    NSString *url = @"http://jpjd.cnaite.cn/mobile/index.php?act=hotel_cleaning&op=bookTask";
    
    [self requestWithMethod:MethodPost UrlString:url params:param completion:^(id json, BOOL isSuccess)
    {
        if (!isSuccess) {
            completion(isSuccess,@"请检查网络");
            return ;
        }
        if (json[@"datas"][@"error"]) {
            completion(false,json[@"datas"][@"error"]);
            return;
        }
        
        completion(isSuccess,json[@"datas"][@"succ"]);
    }];
}
/*
 我的清洁单的网络请求
 **/
- (void)requestCleanOrderListWithCurpage:(NSString *)curpage status:(NSString *)status
                            is_store:(NSString *)is_store
                                is_mouth:(NSString *)is_mouth
                           finishedBlock:(void(^)(BOOL isSuccess, DHCleanOrderDataModel *datasModel))finishedBlock
{
    NSString *url = @"http://jpjd.cnaite.cn/mobile/index.php?act=hotel_cleaning&op=getOrderList";
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"key"] = self.userAccount.key;
    params[@"curpage"] = curpage;
    params[@"status"] = status;
    params[@"is_store"] = is_store;
    params[@"is_mouth"] = is_mouth;

    
    [self requestWithMethod:MethodGet UrlString:url params:params completion:^(id json, BOOL isSuccess)
    {
        if (!isSuccess) {
            finishedBlock(isSuccess,nil);
            return ;
        }
        DHCleanOrderDataModel *datasModel = [DHCleanOrderDataModel yy_modelWithJSON:json];
        datasModel.list = [NSArray yy_modelArrayWithClass:[DHCleanOrderModel class] json:json[@"datas"][@"list"]];
        NSLog(@"%@",json);
        
        finishedBlock(isSuccess,datasModel);
    }];
    
}
#pragma mark: - 我发布的任务网络请求
- (void)requestMyPostTaskWithParam:(NSMutableDictionary *)param completion:(void(^)(BOOL isSuccess,DHCleanOrderDataModel *model))completion
{
    param[@"key"] = self.userAccount.key;
    param[@"is_store"] = @"1";
  
    NSString *url = @"http://jpjd.cnaite.cn/mobile/index.php?act=hotel_cleaning&op=getOrderList";
    
    [self requestWithMethod:MethodGet UrlString:url params:param.copy completion:^(id json, BOOL isSuccess)
    {
        //NSLog(@"%@",json);
        if (!isSuccess) {
            completion(isSuccess,nil);
            return ;
        }
        DHCleanOrderDataModel *orderModel = [DHCleanOrderDataModel yy_modelWithJSON:json];
        orderModel.list = [NSArray yy_modelArrayWithClass:[DHCleanOrderModel class] json:json[@"datas"][@"list"]];
        
        completion(isSuccess,orderModel);
    }];
}
#pragma mark: - 账单明细网络请求
- (void)requestPayLogWithParam:(NSMutableDictionary *)param completion:(void(^)(BOOL isSuccess,DHPayLogModel *model,BOOL hasmore))completion
{
    NSString *url = @"http://jpjd.cnaite.cn/mobile/index.php?act=hotel_cleaning&op=getPayLog";
    
    param[@"key"] = self.userAccount.key;
    param[@"pagesize"] = @"20";
    
    [DHNetworkManager.shareManager requestWithMethod:MethodGet UrlString:url params:param completion:^(id json, BOOL isSuccess)
     {
         if (!isSuccess) {
             completion(isSuccess,false,NO);
             return ;
         }
         //NSLog(@"%@",json);
         DHPayLogModel *paylog = [DHPayLogModel yy_modelWithJSON:json[@"datas"]];
         completion(isSuccess,paylog,[json[@"hasmore"] boolValue]);
         
    }];
}
#pragma mark: - 加载地址列表网络请求
- (void)requestAddressListWithCompletion:(void(^)(BOOL isSuccess, NSArray<DHAddressModel *> *addressList,NSString *errorInfo))completion
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"key"] = self.userAccount.key;
    NSString *url = @"http://jpjd.cnaite.cn/mobile/index.php?act=hotel_cleaning&op=getAddress";
    
    [self requestWithMethod:MethodPost UrlString:url params:params completion:^(id json, BOOL isSuccess) {
        if (!isSuccess) {
            completion(isSuccess,nil,@"网络有问题");
            return ;
        }
        if (json[@"datas"][@"error"]) {
            completion(isSuccess,nil,json[@"datas"][@"error"]);
            return;
        }
        NSArray *addressList = [NSArray yy_modelArrayWithClass:[DHAddressModel class] json:json[@"datas"][@"list"]];
        completion(isSuccess,addressList,nil);
    }];
}


#pragma mark: - 编辑/添加地址网络请求
- (void)postEditAddressWithUrl:(NSString *)url param:(NSMutableDictionary *)param completion:(void(^)(BOOL isSuccess, NSString *statusInfo))completion
{
    param[@"key"] = self.userAccount.key;
  
                     
    [self requestWithMethod:MethodPost UrlString:url params:param.copy completion:^(id json, BOOL isSuccess)
     {
         if (!isSuccess) {
             completion(false,@"请检查网络");
             return ;
         }
         if (json[@"datas"][@"error"]) {
             completion(false,json[@"datas"][@"error"]);
             return;
         }
        // NSLog(@"%@",json);
         completion(isSuccess,json[@"datas"][@"succ"]);
    }];
}
#pragma mark: - 发送评价到服务器网络请求
- (void)postEvaWithParams:(NSDictionary *)params completion:(void(^)(BOOL isSuccess, NSString *statusInfo))completion
{
    NSString *url = @"http://jpjd.cnaite.cn/mobile/index.php?act=hotel_cleaning&op=addEvaluateClear";
    
    [self requestWithMethod:MethodPost
                  UrlString:url params:params
                 completion:^(id json, BOOL isSuccess)
    {
        if (!isSuccess) {
            completion(isSuccess,@"网络有问题");
            return ;
        }
        if (json[@"datas"][@"error"]) {
            completion(false,json[@"datas"][@"error"]);
            return;
        }
        
        completion(isSuccess,json[@"datas"][@"succ"]);
    }];
}
#pragma mark: - 评价列表网络请求 --- 没用
- (void)requestEvaListWithParam:(NSMutableDictionary *)dict
{
    NSString *url = @"http://jpjd.cnaite.cn/mobile/index.php?act=cleaning_info&op=getEvaluateList";
    dict[@"key"] = self.userAccount.key;
    dict[@"type"] = @"2";
    [DHNetworkManager.shareManager requestWithMethod:MethodGet UrlString:url params:dict.copy completion:^(id json, BOOL isSuccess) {
       
        if (!isSuccess) {
            return ;
        }
        
        NSLog(@"%@",json);
    }];
}
/*
 注册账户的网络请求
 **/
- (void)requestToRigestAccountWithUserName:(NSString *)username
                         phone:(NSString *)phone
                        secret:(NSString *)secret
                      password:(NSString *)password
completion:(void(^)(NSString *statusInfo, BOOL isSuccess))completion
{
    if ([username isEqualToString:@""] ||
        [phone isEqualToString:@""] ||
        [secret isEqualToString:@""] ||
        [password isEqualToString:@""])
    {
        // 不能为空
        completion(@"缺少注册参数",false);
        return;
    }
    
    if ([password length] < 3)
    {
        completion(@"密码不能小于3位数",false);
        // 密码长度小于3
        return;
    }
    
    NSString *urlStr = @"http://jpjd.cnaite.cn/mobile/index.php?act=member_secret&op=register";
    
    NSDictionary *params = @{@"username":username,
                             @"phone":phone,
                             @"secret":secret,
                             @"password":password
                             };
    [self requestWithMethod:MethodPost UrlString:urlStr params:params completion:^(id json, BOOL isSuccess)
    {
        if (!isSuccess) {
            // 网络有问题
            completion(@"网络有问题",false);
            return ;
        }
        if (json[@"datas"][@"error"]) {
            completion(json[@"datas"][@"error"],false);
            return;
        }
        completion(json[@"datas"][@"username"],true);
    }];
}
/*
 用户登录请求方法
 **/
- (void)userLogin:(NSString *)userName password:(NSString *)password completion:(void(^)(BOOL isSuccess, NSString *statusInfo))completion
{
    
    NSString *urlStr = @"http://jpjd.cnaite.cn/mobile/index.php?act=login";

    NSDictionary *params = @{@"username":userName,
                             @"password":password,
                             @"client":@"ios"
                             };
//    NSDictionary *params = @{@"username":@"大宝",
//                             @"password":@"123456",
//                             @"client":@"ios"
//                             };

    
    [self requestWithMethod:MethodPost UrlString:urlStr params:params completion:^(id json, BOOL isSuccess) {
        
        if (!isSuccess) {
            completion(isSuccess,@"网络有问题");
            return ;
        }
        
        if (json[@"datas"][@"error"]) {
            //FIXME: - 发送通知
            completion(false,json[@"datas"][@"error"]);
            return;
        }
       // NSLog(@"%@",json[@"datas"]);
        
        self.userAccount = [DHUserAccount yy_modelWithJSON:json[@"datas"]];
        //保存用户数据
        [self.userAccount saveAccount];
        
        completion(isSuccess,@"登录成功");
    }];

}

/**
 首页数据请求
 */
- (void)requestHomeDatasWithCompletion:(void(^)(DHHomeDataModel *datasModel, BOOL isSuccess))completion
{
    
    NSString *urlStr = @"http://jpjd.cnaite.cn/mobile/index.php?act=cleaning_info&op=index";
    
    [self requestWithMethod:MethodGet UrlString:urlStr params:nil completion:^(id json, BOOL isSuccess) {
        
        //NSLog(@"%@",json[@"datas"]);
        if (!isSuccess) {
            completion(nil,isSuccess);
            return ;
        }
        
        DHHomeDataModel *datasModel = [DHHomeDataModel yy_modelWithJSON:json[@"datas"]];
        
        completion(datasModel,isSuccess);
        
    }];
}

@end
